package com.tianze.library.view;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import com.tianze.library.R;

/**
 * Created by zhangl on 2016/3/19
 * 版权所有：苏州天泽信息科技有限公司
 * 自定义弹出窗。
 */
public class PopSheet {

    protected Activity activity;
    protected ViewGroup contentContainer;
    //activity的根View
    private ViewGroup decorView;
    //附加View的根View
    private ViewGroup rootView;
    private OnDismissListener onDismissListener;
    private boolean isDismissing;
    private Animation outAnim;
    private Animation inAnim;
    private int gravity;

    /**
     * PopWindow构造函数。
     *
     * @param activity PopWindow附着的Activity。
     * @param gravity  Pop在屏幕上的位置。只包括顶部和底部两个位置，Gravity.TOP和Gravity.BOTTOM。
     */
    public PopSheet(Activity activity, int gravity) {
        this.activity = activity;
        this.gravity = gravity;
        initViews();
        initAnimation();
    }

    public PopSheet(Activity activity){
        this(activity,Gravity.BOTTOM);
    }

    public void setContentView(int resId) {
        LayoutInflater.from(activity).inflate(resId, contentContainer);
    }

    protected void initAnimation() {
        inAnim = getInAnimation();
        outAnim = getOutAnimation();
    }

    public Animation getInAnimation() {
        int res;
        if (gravity == Gravity.TOP) {
            res = R.anim.tiza_popwindow_top_slide_in;
        } else {
            res = R.anim.tiza_popwindow_bottom_slide_in;
        }
        return AnimationUtils.loadAnimation(activity, res);
    }

    public Animation getOutAnimation() {
        int res;
        if (gravity == Gravity.TOP) {
            res = R.anim.tiza_popwindow_top_slide_out;
        } else {
            res = R.anim.tiza_popwindow_bottom_slide_out;
        }
        return AnimationUtils.loadAnimation(activity, res);
    }

    protected void initViews() {
        LayoutInflater layoutInflater = LayoutInflater.from(activity);
        decorView = (ViewGroup) activity.getWindow().getDecorView()
                                        .findViewById(android.R.id.content);
        rootView = (ViewGroup) layoutInflater.inflate(R.layout.tiza_popwindow, decorView, false);
        rootView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                                              ViewGroup.LayoutParams.MATCH_PARENT
        ));
        contentContainer = (ViewGroup) rootView.findViewById(R.id.content_container);
        contentContainer.setLayoutParams(
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                                             ViewGroup.LayoutParams.WRAP_CONTENT, gravity
                ));
    }

    public void setCancelable(boolean isCancelable) {
        View view = rootView.findViewById(R.id.pop_window);
        if (isCancelable) {
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
        } else {
            view.setOnTouchListener(null);
        }
    }

    /**
     * 显示PopWindow。
     */
    public void show() {
        if (isShowing()) {
            return;
        }
        decorView.addView(rootView);
        isDismissing = false;
        contentContainer.startAnimation(inAnim);
    }

    /**
     * 检测该View是不是已经添加到根视图
     *
     * @return 如果视图已经存在该View返回true
     */
    public boolean isShowing() {
        View view = decorView.findViewById(R.id.pop_window);
        return view != null;
    }

    /**
     * 关闭popwindow。
     */
    public void dismiss() {
        if (isDismissing) {
            return;
        }
        //消失动画
        outAnim.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                decorView.post(new Runnable() {
                    @Override
                    public void run() {
                        //从activity根视图移除
                        decorView.removeView(rootView);
                        isDismissing = true;
                        if (onDismissListener != null) {
                            onDismissListener.onDismiss(PopSheet.this);
                        }
                    }
                });
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
        contentContainer.startAnimation(outAnim);
    }

    public <T extends View> T findViewById(int id) {
        return (T) contentContainer.findViewById(id);
    }

    /**
     * 设置PopWindow消失后回调的监听器。
     *
     * @param onDismissListener
     */
    public void setOnDismissListener(OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

    public interface OnDismissListener {
        void onDismiss(Object o);
    }

}
